using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.Components;
using MacomberMap.Common.Database_Interface;
using MacomberMap.Common.Internals;
using MacomberMap.Common.Types;
using System.Xml;
using MacomberMap.Common.Database_Interface.CIM;
using MacomberMap.Loader.Export;
using System.Reflection;

namespace MacomberMap.Common.Events
{
    /// <summary>
    /// This class encapsualtes the addition of an element into the database
    /// </summary>
    public class MM_Event_NewContingency : MM_Event
    {
        #region Variable Declarations
        /// <summary>The new contingency definition</summary>
        public MM_Contingency_Definition NewContingnecy;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new element event
        /// </summary>
        /// <param name="NewContingnecy">The element to be added</param>        
        public MM_Event_NewContingency(MM_Contingency_Definition NewContingnecy)
        {
            this.NewContingnecy = NewContingnecy;           
        }
        #endregion

        #region List view access
        /// <summary>
        /// Build a list view item 
        /// </summary>
        /// <returns></returns>
        public override ListViewItem BuildListViewItem()
        {
            ListViewItem NewItem;
            if (NewContingnecy == null)
                NewItem = new ListViewItem("?");
            else
                NewItem = new ListViewItem(NewContingnecy.Name + " (" + NewContingnecy.Description + ")");
            
            NewItem.SubItems.Add(this.EventState.ToString());
            NewItem.SubItems.Add(TimeText);
            NewItem.ImageIndex = EventType.Index;
            return NewItem;
        }

        /// <summary>
        /// Update the list view item for this element
        /// </summary>
        /// <param name="lvItem"></param>
        public override void UpdateListViewItem(ListViewItem lvItem)
        {
            lvItem.SubItems[0].Text = NewContingnecy.Name + " (" + NewContingnecy.Description + ")";
            lvItem.SubItems[1].Text = EventState.ToString();
            lvItem.SubItems[2].Text = TimeText;
        }
        #endregion

        #region Details display
        /// <summary>
        /// Display details on an item
        /// </summary>
        /// <param name="OutParent">The pc</param>
        public override void DisplayDetails(Control OutParent)
        {
            TreeView tvOut = new TreeView();
            tvOut.Dock = DockStyle.Fill;
            TreeNode NewNode = this.AddNodeItem("Definition", NewContingnecy.Name, tvOut.Nodes);
            foreach (FieldInfo fI in NewContingnecy.GetType().GetFields())
                this.AddNodeItem(fI.Name, fI.GetValue(NewContingnecy), NewNode.Nodes);

            tvOut.Nodes[0].EnsureVisible();
            OutParent.Controls.Add(tvOut);
        }
        #endregion

        /// <summary>
        /// Add menu items to the new element parameter
        /// </summary>
        /// <param name="OutMenu"></param>
        public override void AddMenuItems(ContextMenuStrip OutMenu)
        {
            (OutMenu.Items.Add("New: " + NewContingnecy.Name + " (" + NewContingnecy.Description + ")") as ToolStripMenuItem).Enabled = false;                
        }

        /// <summary>
        /// Approve the change to the system for this event
        /// </summary>
        /// <param name="ConnectedDatabases">The databases with which the event viewer is connected</param>
        public void ApproveChange(MM_Database[] ConnectedDatabases)
        {
            /*foreach (MM_Database Db in ConnectedDatabases)
                if (Db is MM_Database_Oracle)
                    (Db as MM_Database_Oracle).AddContingency(UInt64.Parse(NewDefinition.DocumentElement.Attributes["BaseElement"].Value), NewDefinition);*/
        }

        /// <summary>
        /// Compare the new event to another event
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(object obj)
        {
            return NewContingnecy.Name.CompareTo((obj as MM_Event_NewContingency).NewContingnecy.Name);                
        }
    }
}
